home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Tools⁄Additions / IntermediatC / Exam 2 #12 / Exam 2.c next >
C/C++ Source or Header  |  1990-11-15  |  4KB  |  176 lines

  1. /*
  2.  
  3. EXAM 2
  4.  
  5. The problem to be solved for this exam is to write the functions necessary to
  6. use a linked list as a "stack" representation.  A stack is similar to a queue,
  7. but with a queue, the waiting is on a first come, first served basis while on a
  8. stack it is "first in, last out."  The next arrival to a stack always goes to
  9. the head of the stack (not to the end).  When a value is removed from the stack,
  10. it is always removed from the head.  It could turn out that a person waiting in
  11. a stack for service would never get serviced because of constant arrivals of new
  12. people, but a stack is not intended for human waiting lines.
  13.  
  14. The stack is to be composed of any number of links that are created and placed
  15. at the head of a linked list.  Links are removed only from the head.
  16.  
  17. For this quiz, you may write all the functions in a single C file.
  18.  
  19. You must include this declaration in your file:
  20.  
  21.     typedef struct link
  22.        {
  23.        char letter;
  24.        struct link * next;
  25.        } link, * link_ptr;
  26.  
  27. and write a main() function that will accomplish the following:
  28.  
  29.     (1)    loop five times;  adding the letters 'a', 'b', 'c', 'd', 'e'
  30.         to a stack pointed to by a regional variable called "stack"
  31.     (2)    will test the stack just created to see if it is empty.
  32.         If the test function returns true, then printf "empty stack"
  33.         else printf "stack has entries".  (You know it is not empty,
  34.         but test it anyway.)
  35.     (3)    will printf "d is on stack" if the character 'd' is in one
  36.         of the links in the stack.  (Of course, it will be, but test
  37.         for it anyway.)
  38.     (4)    will remove all the links from the stack
  39.         and printf the characters in each link -- in this exam the
  40.         output would be: e d c b a.
  41.  
  42. To support main() you must write the following functions:
  43.  
  44.     push:        to place a link at the head of the stack.  The link should
  45.                 contain a character passed to this function.
  46.             
  47.     pop:        to remove the top most link from the stack.  The function
  48.                 should return the character that was in the link.  If the
  49.                 stack is empty, return the null character.  Remember to free
  50.                 the link after removal.
  51.             
  52.     empty:        to return a boolean value indicating whether the stack
  53.                 is empty or not.
  54.             
  55.     in_stack:    to return a boolean value -- true if the character
  56.                 specified as the parameter is present anywhere
  57.                 on the stack.
  58.  
  59. */
  60.  
  61.  
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64.  
  65.  
  66. #define LOOPS            5
  67. #define FIRST_LETTER    'a'
  68. #define D_CHAR            'd'
  69. #define TRUE            0xff
  70. #define FALSE            0x00
  71.  
  72.  
  73. typedef struct link
  74.    {
  75.    char letter;
  76.    struct link * next;
  77.    } link, * link_ptr;
  78.    
  79.    
  80. static link * head = NULL;
  81.  
  82.  
  83. static void push(char value);
  84. static char pop(void);
  85. static char empty(void);
  86. static char in_stack(char value);
  87.  
  88.  
  89. main()
  90.    {
  91.    int i;
  92.    char value;
  93.    
  94.    value = FIRST_LETTER;
  95.    for (i=0; i<LOOPS; i++)
  96.       push(value++);
  97.    if (empty())
  98.       printf("empty stack\n");
  99.    else
  100.       printf("stack has entries\n");
  101.    if (in_stack(D_CHAR))
  102.       printf("d is on stack\n");
  103.    for (i=0; i<5; i++)
  104.       printf("%c ", pop());
  105.    }
  106.  
  107.  
  108. static void push(char value)
  109.    {
  110.    link * new_link;
  111.    
  112.    new_link = (link *) malloc(1 * sizeof(link));
  113.    new_link->letter = value;
  114.    if (!head)
  115.       {
  116.       head = new_link;            /* the first element in the stack's next pointer */
  117.       head->next = new_link;    /* always points to itself */
  118.       }
  119.    else
  120.       {
  121.       new_link->next = head;
  122.       head = new_link;
  123.       }
  124.    }
  125.    
  126.    
  127. static char pop(void)
  128.    {
  129.    link * delete_link;
  130.    char value;
  131.    
  132.    if (!head)
  133.       return 0;                    /* empty stack */
  134.    if (head == head->next)        /* only 1 element on the stack */
  135.       {
  136.       value = head->letter;
  137.       free(head);
  138.       head = NULL;
  139.       return value;
  140.       }
  141.    else                            /* more than 1 element on stack */
  142.       {
  143.       value = head->letter;
  144.       delete_link = head;
  145.       head = head->next;
  146.       free(delete_link);
  147.       return value;
  148.       }
  149.    }
  150.  
  151.  
  152. static char empty(void)
  153.    {
  154.    if (!head)
  155.       return TRUE;
  156.    else
  157.       return FALSE;
  158.    }
  159.  
  160.  
  161. static char in_stack(char value)
  162.    {
  163.    link * next_link;
  164.    
  165.    next_link = head;
  166.    while (next_link != next_link->next)    /* loop until an element is found that points to */
  167.       {                                    /* itself, this is the first element on the stack */
  168.       if (next_link->letter == value)
  169.          return TRUE;
  170.       next_link = next_link->next;
  171.       }
  172.    if (next_link->letter == value)        /* check the first element of the stack */
  173.       return TRUE;
  174.    return FALSE;
  175.    }
  176.